package com.lakienko.igor.random.values.rest;

import com.lakienko.igor.random.values.rest.convert.RandomsConverters;
import com.lakienko.igor.random.values.rest.model.in.CategoryRestIn;
import com.lakienko.igor.random.values.rest.model.out.CategoryRestOut;
import com.lakienko.igor.random.values.service.RandomValuesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v1/random/category")
public class CategoryController implements RandomsConverters {

    private final RandomValuesService randomValuesService;

    @GetMapping
    List<CategoryRestOut> getAllCategories() {
        return randomValuesService.getAllCategories()
                .stream()
                .map(this::convert)
                .toList();
    }

    @PostMapping("/add")
    CategoryRestOut addNewCategory(@RequestBody CategoryRestIn categoryRestIn) {
        return convert(randomValuesService.addNewCategory(convert(categoryRestIn)));
    }

    @PutMapping("/update/{categoryId}")
    CategoryRestOut updateCategory(@RequestBody CategoryRestIn categoryRestIn,
                                   @PathVariable UUID categoryId) {
        return convert(randomValuesService.updateCategory(convert(categoryRestIn, categoryId)));
    }

    @DeleteMapping("/delete/{categoryId}")
    void deleteCategory(@PathVariable UUID categoryId) {
        randomValuesService.deleteCategory(categoryId);
    }

}
